import React, { Component } from 'react'
import {ListBox} from '../../../components'
import UpdateLocationPage from './Update'
import { Modal } from 'antd';
import LocationForm from './LocationForm'
import { fetchData } from '../../../components/AjaxConsumer'
const confirm = Modal.confirm;

export default class ListLocationPage extends Component {
    constructor(props) {
        super(props);
        this.state = {
            loading: false,
            visible: false,
            addFlag: false,
            formData:{},
            submitUrl:'',
            initform:false
        }
        //行操作列表
        this.rowOperation = [
        ];
        //header 操作列表
        this.headerOperation = [
            {
                handleName: 'addLocation',
                showName: '新增',
                iconClass: 'plus'
            },{
                handleName: 'edit',
                showName: '修改',
                iconClass: 'edit'
            },{
                handleName: 'delete',
                showName: '删除',
                iconClass: 'delete'
            }
        ];


        //table列表展示列配置
        //showName、columnName 必有属性， columnWidth 可选，默认100, columnHidden: true 列隐藏
        this.tableColumn = {
            storageKey: 'ListLocationColumnConfig',
            data: [
                {
                    showName: 'locationId',
                    columnName: 'locationId',
                    columnHidden:true,
                    foreverHidden: true
                },{ showName: '库位编码',
                    columnName: 'locationCode'
                },{
                    showName: '库区编码',
                    columnName: 'areaCode'
                },{
                    showName: '区域编码',
                    columnName: 'regionCode'
                },{
                    showName: '仓库编码',
                    columnName: 'warehouseCode'
                },{
                    showName: '工作区',
                    columnName: 'workArea',
                },{
                    showName: '创建人',
                    columnName: 'createBy',
                },{
                    showName: '创建时间',
                    columnName: 'gmtCreate',
                }
                //,{
                //    showName: '上架顺序',
                //    columnName: 'shelfOrder'
                //},{
                //    showName: '盘点顺序',
                //    columnName: 'checkOrder'
                //},{
                //    showName: '拣货顺序',
                //    columnName: 'pickOrder'
                //},{
                //    showName: '库位使用',
                //    columnName: 'locationUse'
                //},{
                //    showName: '是否开启库内交接',
                //    columnName: 'isOpenKnFlag',
                //    columnWidth: 150,
                //    showOption:[
                //        {
                //            optionKey:'',
                //            optionShow:''
                //        },{
                //            optionKey:'0',
                //            optionShow:'否'
                //        },{
                //            optionKey:'1',
                //            optionShow:'是'
                //        }
                //    ]
                //},{
                //    showName: '是否允许拣货',
                //    columnName: 'pickFlag',
                //    columnWidth: 150,
                //    showOption:[
                //        {
                //            optionKey:'',
                //            optionShow:''
                //        },{
                //            optionKey:'0',
                //            optionShow:'否'
                //        },{
                //            optionKey:'1',
                //            optionShow:'是'
                //        }
                //    ]
                //},{
                //    showName: '库位类型',
                //    columnName: 'locationType'
                //},{
                //    showName: '库位属性',
                //    columnName: 'locationAttr'
                //},{
                //    showName: '周转需求',
                //    columnName: 'turnDemand'
                //},{
                //    showName: '库存环境',
                //    columnName: 'stockEnv'
                //},{
                //    showName: '校验码',
                //    columnName: 'checkCode'
                //},{
                //    showName: '体积限制',
                //    columnName: 'volumeLimit'
                //},{
                //    showName: '重量限制',
                //    columnName: 'weightLimit'
                //},{
                //    showName: '箱数限制',
                //    columnName: 'boxLimit'
                //},{
                //    showName: '数量限制',
                //    columnName: 'qtyLimit'
                //},{
                //    showName: '托盘限制',
                //    columnName: 'palletLimit'
                //},{
                //    showName: '长度',
                //    columnName: 'length'
                //},{
                //    showName: '宽度',
                //    columnName: 'width'
                //},{
                //    showName: '高度',
                //    columnName: 'height'
                //},{
                //    showName: 'X坐标',
                //    columnName: 'xcoordinate'
                //},{
                //    showName: 'Y坐标',
                //    columnName: 'ycoordinate'
                //},{
                //    showName: 'Z坐标',
                //    columnName: 'zcoordinate'
                //},{
                //    showName: '排',
                //    columnName: 'locRow'
                //},{
                //    showName: '层数',
                //    columnName: 'layerNum'
                //},{
                //    showName: '列',
                //    columnName: 'locColumn'
                //},{
                //    showName: '巷道',
                //    columnName: 'bord'
                //},{
                //    showName: '允许混放产品',
                //    columnName: 'productMix',
                //    columnWidth: 130
                //},{
                //    showName: '允许混放批次',
                //    columnName: 'batchMix',
                //    columnWidth: 130
                //}
            ]
        };

        //搜索属性配置，如果字段不配置，默认是input搜索
        // 搜索类型：
        //      1、输入框 type='input'， 默认
        //      2、时间 type='date'
        //      3、数字 type='number'
        //      4、下拉框 type='select'
        this.searchComponentData = [

            {
                name: 'pickFlag',
                type: 'select',
                optionList:[
                    {
                        optionKey:'',
                        optionShow:''
                    },
                    {
                        optionKey:'0',
                        optionShow:'否'
                    },{
                        optionKey:'1',
                        optionShow:'是'
                    }
                ]
            },
            {
                name: 'isOpenKnFlag',
                type: 'select',
                optionList:[
                    {
                        optionKey:'',
                        optionShow:''
                    },
                    {
                        optionKey:'0',
                        optionShow:'否'
                    },{
                        optionKey:'1',
                        optionShow:'是'
                    }
                ]
            },
            {
                name:'gmtCreate',
                type: 'date',
                maxName:'gmtCreateMax',
                minName:'gmtCreateMin',
                showTime:'1'
            }

            ];

    }

    rowHandles = (strHandles, rowObj) => {
        if(strHandles === 'edit'){
            this.editHandle(rowObj);
        }else if(strHandles === 'delete'){
            this.deleteHandle(rowObj);
        }else if(strHandles === 'addLocation'){
            this.addLocationHandle();
        }
    }
    // 增加用户
    addLocationHandle = () => {
        //this.refs.updateLocation.addLocationHandle();
        this.setState({
            formData:{},
            submitUrl:'/base/data/location/c',
        })
        this.showModal();
    }
    editHandle = (rowObj) => {
         if(rowObj.length == 0){
            Modal.error({
                title: '错误提示',
                content: '请选择一条数据',
            });
        }else if(rowObj.length > 1){
            Modal.error({
                title: '错误提示',
                content: '只能选择一条数据',
            });
        }else {

            // this.refs.updateLocation.editHandle(rowObj[0].data);
            //在这里做ajax，把得到的值，set到formData
            /*  在回调中拿到res
            *       this.setState({
            *           formData: res.data
            *       })
            * */
            //this.editHandle(rowObj[0].data);

             let id = rowObj[0].data.locationId;

             let options={
                 url:'/base/data/location/r/'+id,
                 type: 'get',
                 condition:{}
             };

             fetchData(this, options, (resData) => {
                 if (resData.code == '1'){
                     this.setState({
                         formData:resData.data,
                         submitUrl:'/base/data/location/u'
                     });
                     this.showModal();
                 }else {
                     message.error(resData.msg);
                 }
             });

        }

    }
    //显示弹框
        showModal = () => {
        this.setState({
            visible: true,
        });
    }
    editGetInfoCallBack = (resData) =>{
        this.setState({
            submitUrl:'/base/data/location/u',
            resData: resData
        });
        this.showModal();//显示弹框
    }

    // 编辑信息
    editHandle2222 = (rowObj) => {
        this.setState({resData: null});
        //根据对象的Id 属性获取对象信息
        //alert(rowObj)
        let locationId = rowObj.locationId;
        //alert(locationId)
        const options= {
            url: '/base/data/location/r/'+locationId,
            type: 'get',//请求方式
            condition:{}
        };
        fetchData(this, options, this.editGetInfoCallBack);
    }

    deleteHandle = (rowObj) => {
        if(rowObj.length == 0){
            Modal.error({
                title: '错误提示',
                content: '请选择一条数据',
            });
        }else if(rowObj.length > 1){
            Modal.error({
                title: '错误提示',
                content: '只能选择一条数据',
            });
        }else {
            //this.refs.updateLocation.deleteHandle(rowObj[0].data)

            console.log('--shanchu---',rowObj);
            let locationId = rowObj[0].data.locationId;
            console.log(locationId);
            let options= {
                url: '/base/data/location/d',
                condition:{locationId:locationId}
            };

            let _that = this;
            confirm({
                title: '您确定要删除吗？',
                onOk() {
                    fetchData(_that, options, (resData) => {
                        console.log(resData);
                        _that.setState({
                            resData: resData
                        });

                        _that.refreshHandle();
                        if (resData.code == '1'){
                            message.success('删除成功！');
                            _that.refreshHandle();
                        }else {
                            message.error(resData.msg);
                        }
                    });
                },
                onCancel() {
                    console.log("取消了删除操作")
                },
            });
        }
    }

    handleCancel = () => {
        this.setState({
            visible: false,
            initform: true,
        });
    }
    initformCancel = () => {
        this.setState({
            initform: false,
        });
    }
    assignRoleHandle = (rowObj) =>{
        //传递参数--用户id
        let locationId = rowObj.locationId;
        this.refs.assignRole.initModal(locationId);
    }

    //刷新列表
    refreshHandle = () => {
        this.refs.listLocation.refreshHandle();
    }

    render() {
        //当左侧隐藏时，列表宽度加上左侧的宽度，
        //1、当前this.props.isHideLeft 是从页面组件containers/Main/main.js传过来
        //2、this.props.isHideLeft 最顶级赋值是在页面 containers/Main/main.js
        let isHideLeft = this.props.isHideLeft;

        return(
            <div>
                <ListBox
                    ref = 'listLocation'
                    isHideLeft={isHideLeft}
                    tableColumnData = {this.tableColumn}
                    title=" 库位管理"
                    searchComponentData={this.searchComponentData}
                    url = '/base/data/location/list'
                    rowHandles = {this.rowHandles}
                    rowOperation = {this.rowOperation}
                    headerOperation = {this.headerOperation}
                    isRowSingle = "true"
                />

                <Modal
                    style={{top: '20px'}}
                    visible={this.state.visible}
                    title="库位维护"
                    onOk={this.handleOk}
                    onCancel={this.handleCancel}
                    width={'750px'}
                    footer=''
                    maskClosable={false}>
                    <LocationForm
                        onCancel={this.handleCancel}
                        ref="locationForm"
                        submitUrl={this.state.submitUrl}
                        formData={this.state.formData}
                        init={this.state.visible}
                        refreshHandle={this.refreshHandle}
                        initform = {this.state.initform}
                        initformCancel = {this.initformCancel}
                    />
                </Modal>


            </div>
        )
    }
}
